<template>
  <div class="app-container">
    <el-row :gutter="15">
      <el-form ref="form" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="发件人邮箱" prop="sendAddress">
          <el-input v-model="formData.sendAddress" placeholder="请输入发件人邮箱" :style="{width: '25%'}"></el-input>
        </el-form-item>
        <el-form-item label="收件人邮箱" prop="receiveAddress">
          <el-input v-model="formData.receiveAddress" placeholder="请输入收件人邮箱" autocomplete="off" clearable
                    :style="{width: '25%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="抄送人邮箱" prop="cc">
          <el-input v-model="formData.cc" placeholder="请输入抄送人邮箱" autocomplete="off" clearable :style="{width: '25%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="邮箱密码" prop="sendPassword">
          <el-input v-model="formData.sendPassword" placeholder="请输入邮箱密码" autocomplete="off" clearable show-password
                    :style="{width: '25%'}"></el-input>
        </el-form-item>
        <el-form-item label="邮件主题" prop="mailTitle">
          <el-input v-model="formData.mailTitle" placeholder="请输入邮件主题" clearable :style="{width: '25%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="附件">
          <input type="file" id="file" hidden @change="fileChange">
          <el-input placeholder="请输入内容" v-model="formData.imgSavePath" :style="{width: '25%'}"
                    class="input-with-select">
            <el-button slot="append" icon="el-icon-folder" type="success" @click="btnChange"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item prop="content" :style="{width: '75%'}">
          <quill-editor class="editor"
                        ref="myTextEditor"
                        v-model="formData.content"
                        :options="editorOption"
                        @blur="onEditorBlur($event)"
                        @focus="onEditorFocus($event)"
                        @ready="onEditorReady($event)"
                        @change="onEditorChange($event)">
          </quill-editor>
        </el-form-item>
      </el-form>
    </el-row>
    <el-col :span="24" align="right">
      <el-button type="primary" @click="handleSend" v-hasPermi="['mail:info:send']">发 送</el-button>
    </el-col>
    <el-upload
      class="avatar-uploader"
      :action="imageServerIp"
      :on-success="handleAvatarSuccess"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      style="display:none">
      <img v-if="imageUrl" :src="imageUrl" class="avatar"/>
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
  </div>
</template>
<script>
    import {sendCustomerEmail} from "../../../api/mail/mail";
    import Quill from 'quill';
    import {quillEditor} from "vue-quill-editor";
    // 对齐方式
    let Parchment = Quill.import('parchment');
    let options = {
        scope: Parchment.Scope.BLOCK,
        whitelist: ['left', 'center', 'right'],
    };
    let AlignStyle = new Parchment.Attributor.Style('align', 'text-align', options);
    Quill.register(AlignStyle, true);

    export default {
        name: 'Home',
        components: {
            quillEditor
        },
        data() {
            return {
                formData: {
                    //imgSavePath: '',
                    sendAddress: '',
                    receiveAddress: '',
                    sendPassword: '',
                    mailTitle: '',
                    content: '',
                    cc: []
                },
                imageServerIp: '',
                imageUrl: '',
                input: '',
                rules: {
                    sendAddress: [{
                        required: true,
                        message: '请输入发件人邮箱',
                        trigger: 'blur'
                    }],
                    receiveAddress: [{
                        required: true,
                        message: '请输入收件人邮箱',
                        trigger: 'blur'
                    }],
                    sendPassword: [{
                        required: true,
                        message: '请输入邮箱密码',
                        trigger: 'blur'
                    }],
                    mailTitle: [{
                        required: true,
                        message: '请输入邮件主题',
                        trigger: 'blur'
                    }],
                    content: [{
                        required: true,
                        message: '',
                        trigger: 'blur'
                    }],
                },
                editorOption: {
                    modules: {
                        toolbar: {
                            container: [
                                ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
                                ["blockquote", "code-block"], // 引用  代码块
                                [{header: 1}, {header: 2}], // 1、2 级标题
                                [{list: "ordered"}, {list: "bullet"}], // 有序、无序列表
                                [{script: "sub"}, {script: "super"}], // 上标/下标
                                [{indent: "-1"}, {indent: "+1"}], // 缩进
                                // [{'direction': 'rtl'}],                         // 文本方向
                                [{size: ["small", false, "large", "huge"]}], // 字体大小
                                [{header: [1, 2, 3, 4, 5, 6, false]}], // 标题
                                [{color: []}, {background: []}], // 字体颜色、字体背景颜色
                                [{font: []}], // 字体种类
                                [{align: []}], // 对齐方式
                                ["clean"], // 清除文本格式
                                ["image", "video"] // 链接、图片、视频
                            ], //工具菜单栏配置
                            handlers: {
                                image: function (value) {
                                    if (value) {
                                        // 用upload的点击事件代替文本编辑器的图片上传事件
                                        document.getElementsByClassName('el-upload__input')[0].click()
                                    } else {
                                        this.quill.format('image', false)
                                    }
                                }
                            }
                        }
                    },
                    placeholder: '请在这里添加邮件内容', //提示
                    readyOnly: false, //是否只读
                    theme: 'snow', //主题 snow/bubble
                    syntax: true, //语法检测
                }
            }
        },
        methods: {
            handleAvatarSuccess(file, res) {
                // this.imageUrl = URL.createObjectURL(file.raw);
                const that = this;
                //editor对象
                const quill = this.$refs.myTextEditor.quillEditor;
                // 如果上传成功
                if (res.response.code === '0') {
                    // 获取光标所在位置
                    const length = quill.selection.savedRange.index;
                    // 插入图片  res.info为服务器返回的图片地址
                    quill.insertEmbed(length, 'image', res.response.datas);
                    // 调整光标到最后
                    quill.setSelection(length + 1)
                } else {
                    that.$Message.error('图片插入失败')
                }
                that.quillUpdateImg = false
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isJPG) {
                    this.$message.error('上传图片只能是JPG/PNG格式!')
                }
                if (!isLt2M) {
                    this.$message.error('上传图片大小不能超过 2MB!')
                }
                return isJPG && isLt2M
            },
            onEditorReady() {
            },
            fileChange(e) {
                try {
                    const fu = document.getElementById('file');
                    if (fu == null) return;
                    this.form.imgSavePath = fu.files[0].path;
                    console.log(fu.files[0].path)
                } catch (error) {
                    console.debug('choice file err:', error)
                }
            },
            btnChange() {
                var file = document.getElementById('file');
                file.click()
            },
            // 表单重置
            reset() {
                this.formData = {
                    sendAddress: undefined,
                    receiveAddress: undefined,
                    sendPassword: undefined,
                    mailTitle: undefined,
                    content: undefined,
                    cc: undefined
                };
                this.resetForm("form");
            },
            // 失去焦点
            onEditorBlur(editor) {
            },
            // 获得焦点
            onEditorFocus(editor) {
            },
            // 开始
            onEditorReady(editor) {
            },
            // 值发生变化
            onEditorChange(editor) {
                this.content = editor.html;
                console.log(editor);
            },
            handleSend() {
                console.log(this.formData);
                sendCustomerEmail(this.formData).then(res => {
                    this.$modal.msgSuccess("发送成功");
                })
            }
        },
        computed: {
            editor() {
                return this.$refs.myTextEditor.quillEditor;
            }
        },
        mounted() {
            console.log("11", this.$route.query.getEmial);
            const receiveAddress = this.$route.query.getEmial.join(',');
            this.$set(this.formData, "receiveAddress", receiveAddress)
            // console.log('this is my editor',this.editor);
        }
    }
</script>
<style>
  .editor {
    line-height: normal !important;
    height: 300px;
  }

  .ql-snow .ql-tooltip[data-mode=link]::before {
    content: "请输入链接地址:";
  }

  .ql-snow .ql-tooltip.ql-editing a.ql-action::after {
    border-right: 0px;
    content: '保存';
    padding-right: 0px;
  }

  .ql-snow .ql-tooltip[data-mode=video]::before {
    content: "请输入视频地址:";
  }

  .ql-snow .ql-picker.ql-size .ql-picker-label::before,
  .ql-snow .ql-picker.ql-size .ql-picker-item::before {
    content: '14px';
  }

  .ql-snow .ql-picker.ql-size .ql-picker-label[data-value=small]::before,
  .ql-snow .ql-picker.ql-size .ql-picker-item[data-value=small]::before {
    content: '10px';
  }

  .ql-snow .ql-picker.ql-size .ql-picker-label[data-value=large]::before,
  .ql-snow .ql-picker.ql-size .ql-picker-item[data-value=large]::before {
    content: '18px';
  }

  .ql-snow .ql-picker.ql-size .ql-picker-label[data-value=huge]::before,
  .ql-snow .ql-picker.ql-size .ql-picker-item[data-value=huge]::before {
    content: '32px';
  }

  .ql-snow .ql-picker.ql-header .ql-picker-label::before,
  .ql-snow .ql-picker.ql-header .ql-picker-item::before {
    content: '文本';
  }

  .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="1"]::before,
  .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="1"]::before {
    content: '标题1';
  }

  .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="2"]::before,
  .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="2"]::before {
    content: '标题2';
  }

  .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="3"]::before,
  .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="3"]::before {
    content: '标题3';
  }

  .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="4"]::before,
  .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="4"]::before {
    content: '标题4';
  }

  .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="5"]::before,
  .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="5"]::before {
    content: '标题5';
  }

  .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="6"]::before,
  .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="6"]::before {
    content: '标题6';
  }

  .ql-snow .ql-picker.ql-font .ql-picker-label::before,
  .ql-snow .ql-picker.ql-font .ql-picker-item::before {
    content: '标准字体';
  }

  .ql-snow .ql-picker.ql-font .ql-picker-label[data-value=serif]::before,
  .ql-snow .ql-picker.ql-font .ql-picker-item[data-value=serif]::before {
    content: '衬线字体';
  }

  .ql-snow .ql-picker.ql-font .ql-picker-label[data-value=monospace]::before,
  .ql-snow .ql-picker.ql-font .ql-picker-item[data-value=monospace]::before {
    content: '等宽字体';
  }
</style>
